package com.wing.api.add.storesRequisition.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.wing.api.ApplicationContextUtil;
import com.wing.api.add.storesRequisition.BaseStoresRequisitionSeriesService;
import com.wing.cabinet.model.request.StoresRequisitionForm;
import com.wing.cabinet.model.request.StoresRequisitionProductForm;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.cabinet.service.StoresRequisitionService;
import com.wing.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 标准领新申请接口
 */
@Service
@Slf4j
public class StandardStoresRequisitionServiceImpl implements BaseStoresRequisitionSeriesService {

    @Transactional
    @Override
    public void logicProcess(JSONObject jsonObject, EnterpricceIdVO enterpricceIdVo) {
        Gson gson = new Gson();
        StoresRequisitionForm storesRequisitionForm = gson.fromJson(jsonObject.toJSONString(), StoresRequisitionForm.class);
        log.info("请求参数,{}", jsonObject.toJSONString());
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）
        String sourceSign = jsonObject.getString("sign"); // 签名值
        if(StringUtils.isBlank(sourceSign)){
            throw new BusinessException("签名值为空！");
        }
        if(StringUtils.isBlank(storesRequisitionForm.getCabinetCode())){
            throw new BusinessException("缺少主柜编号参数！");
        }
        if(null == storesRequisitionForm.getCabinetId()){
            throw new BusinessException("缺少主柜ID参数！");
        }
        if(null == storesRequisitionForm.getUserId()){
            throw new BusinessException("缺少领取人ID参数！");
        }
        if(storesRequisitionForm.getProductDetail().size() == 0){
            throw new BusinessException("缺少物料数据集合！");
        }
        for (StoresRequisitionProductForm storesRequisitionProductForm : storesRequisitionForm.getProductDetail()) {
            if(null == storesRequisitionProductForm.getSkuCode()){
                throw new BusinessException("缺少物料编号参数！");
            }
            if(null == storesRequisitionProductForm.getPackId()){
                throw new BusinessException("缺少包装ID参数！");
            }
            if(null == storesRequisitionProductForm.getBaseRate()){
                throw new BusinessException("缺少换算率参数！");
            }
            if(null == storesRequisitionProductForm.getShipmentQuantity()){
                throw new BusinessException("缺少出货数量参数！");
            }
        }

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(jsonObject.get("cabinetCode"));
        tempStr.append(jsonObject.get("cabinetId"));
        tempStr.append(jsonObject.get("userId"));
        tempStr.append(jsonObject.getJSONArray("productDetail"));
        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sourceSign)){
            throw new BusinessException("签名不通过！");
        }

        StoresRequisitionService storesRequisitionService = (StoresRequisitionService) ApplicationContextUtil.getBean("storesRequisitionServiceImpl");
        storesRequisitionService.submitStoresRequisitionForm(storesRequisitionForm);
    }

    public static void main(String[] args) {
//        String key = "$2a$10$fxpgo/SLFHCeNThnfF/q7uvoZJOxbJBNNI4.dshzlntgBIHKI9eb.";
        /*String cabinetCode = "N_ZG_20240205_01-20240205-1";
        String cabinetId = "1561613804873713044";
        String userId = "1766014412977647617";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cabinetCode", cabinetCode);
        jsonObject.put("cabinetId", cabinetId);
        jsonObject.put("userId", userId);
        List<LinkedHashMap<String, Object>> list = new ArrayList<>();

        JSONArray jsonArray = new JSONArray();
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("skuCode", "SKU-03");
        map.put("packId", "1566986680428109835");
        map.put("baseRate", 10);
        map.put("shipmentQuantity", 1);
        map.put("remark", "test");
        jsonArray.add(map);*/
//        System.out.println(map);


        /*JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("skuCode", "SKU-03");
        jsonObject1.put("packId", "1566986680428109835");
        jsonObject1.put("baseRate", 10);
        jsonObject1.put("shipmentQuantity", 1);
        jsonObject1.put("remark", "test");
        jsonArray.add(jsonObject1);
        System.out.println(jsonObject1);*/


        String key = "$2a$10$A4q7sKe4/3GekaIIHLTqG.Gd4y8PhX4yLNleuHK9fvvyWq1vodM6e";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page", 1);
        jsonObject.put("limit", 1000);
//        jsonObject.put("nickname", "罗剑锋");

//        jsonObject.put("productDetail", list);
//        jsonObject.put("productDetail", jsonArray);

        StringBuffer tempStr = new StringBuffer();
//        tempStr.append(jsonObject.get("cabinetCode"));
//        tempStr.append(jsonObject.get("cabinetId"));
//        tempStr.append(jsonObject.get("userId"));
//        tempStr.append(jsonObject.getJSONArray("productDetail"));
        tempStr.append(jsonObject.get("page"));
        tempStr.append(jsonObject.get("limit"));
//        tempStr.append(jsonObject.get("nickname"));
        tempStr.append(key);
//        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
//        log.info("签名目的串,{}", desSign);
        System.out.println(desSign);

    }
}
